package com.b2b.message.admin.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.b2b.bean.ResultBase;
import com.b2b.constant.ResultEnum;
import com.b2b.message.admin.bean.UserBean;
import com.b2b.message.admin.constant.Constant;
import com.b2b.message.admin.plus.service.IMailConfigService;
import com.b2b.message.admin.plus.service.IMailLoggerService;
import com.b2b.message.admin.plus.service.IMailTemplateService;
import com.b2b.message.admin.plus.service.IMakeMessageService;
import com.b2b.message.admin.plus.service.INoticeTemplateService;
import com.b2b.message.admin.plus.service.INoticeUserService;
import com.b2b.message.admin.plus.service.ISmsConfigService;
import com.b2b.message.admin.plus.service.ISmsLoggerService;
import com.b2b.message.admin.plus.service.ISmsTemplateService;
import com.b2b.message.admin.plus.service.ISystemTemplateService;
import com.b2b.message.admin.plus.service.ISystemUserService;
import com.b2b.message.admin.plus.service.IWxConfigService;
import com.b2b.message.admin.plus.service.IWxContentTemplateService;
import com.b2b.message.admin.plus.service.IWxLoggerService;
import com.b2b.message.admin.plus.service.IWxTemplateService;
import com.b2b.message.admin.plus.service.IWxUserFormService;
import com.b2b.utils.Tools;
import com.baomidou.mybatisplus.plugins.Page;

/**
 * 版权：上海极和信息科技有限公司
 * 功能：超级父类 - 公用controller
 * 作者：朱伟峰
 * 邮箱：zhuweifeng24@gmail.com
 * 日期：2017年1月13日
 */
@Controller
public class SuperController
{
	private static final Logger logger = LoggerFactory.getLogger(SuperController.class);

	@Autowired
	protected HttpServletRequest request;
	@Autowired
	protected HttpServletResponse response;
	@Autowired
	protected HttpSession session;
	@Autowired
	protected IMailConfigService mailConfigService;
	@Autowired
	protected IMailLoggerService mailLoggerService;
	@Autowired
	protected IMailTemplateService mailTemplateService;
	@Autowired
	protected IMakeMessageService makeMessageService;
	@Autowired
	protected INoticeTemplateService noticeTemplateService;
	@Autowired
	protected INoticeUserService noticeUserService;
	@Autowired
	protected ISmsConfigService smsConfigService;
	@Autowired
	protected ISmsLoggerService smsLoggerService;
	@Autowired
	protected ISmsTemplateService smsTemplateService;
	@Autowired
	protected ISystemTemplateService systemTemplateService;
	@Autowired
	protected ISystemUserService systemUserService;
	@Autowired
	protected IWxConfigService wxConfigService;
	@Autowired
	protected IWxContentTemplateService wxContentTemplateService;
	@Autowired
	protected IWxLoggerService wxLoggerService;
	@Autowired
	protected IWxTemplateService wxTemplateService;
	@Autowired
	protected IWxUserFormService wxUserFormService;
	
	/**
	 * 返回result对象
	 * @return
	 */
	protected ResultBase resultSuccess()
	{
		return new ResultBase().success();
	}
	
	/**
	 * 返回result对象
	 * @return
	 */
	protected ResultBase resultBase(ResultEnum resultEnum)
	{
		ResultBase result = new ResultBase();
		result.setCode(resultEnum.getCode());
		result.setMessage(resultEnum.getMessage());
		return result;
	}
	
	/**
	 * 返回result异常对象
	 * @return
	 */
	protected ResultBase resultCatch()
	{
		return this.resultBase(ResultEnum.ERR_CATCH);
	}
	
	/**
	 * 获取框架分页信息
	 * @param request
	 * @return
	 */
	protected <T> Page<T> getPage()
	{
		return new Page<T>(this.getCurrent(), this.getSize());
	}
	
	/**
	 * 获取当前页
	 * @param request
	 * @return
	 */
	protected int getCurrent()
	{
		String current = this.request.getParameter("current");
		if (Tools.isTrimEmpty(current)) 
			current = "1";
		return Tools.genInt(current);
	}
	
	/**
	 * 获取当前显示条数
	 * @param request
	 * @return
	 */
	protected int getSize()
	{
		String size = this.request.getParameter("size");
		if (Tools.isTrimEmpty(size))
			return Constant.PageSize;
		return Tools.genInt(size);
	}
	
	/**
	 * 获取请求次数
	 * @param request
	 * @return
	 */
	protected int getDraw()
	{
		String draw = this.request.getParameter("draw");
	    if (Tools.isTrimEmpty(draw)) draw = "1";
		return Tools.genInt(draw);
	}
	
	/**
	 * 获取请求次数
	 * @param request
	 * @return
	 */
	protected boolean getIsAsc()
	{
		String isAsc = this.request.getParameter("isAsc");
	    if (Tools.isTrimEmpty(isAsc)) 
	    	isAsc = "asc";
		return (("ASC").equals(isAsc.toUpperCase())) ? true : false;
	}
	
	/**
	 * shiro Subject
	 * @return
	 */
	protected Subject getSubject()
	{
		return SecurityUtils.getSubject();
	}
	
	/**
	 * 获取当前登录用户UserBean对象
	 * @return
	 */
	protected UserBean getUserBean()
	{
		UserBean userBean = null;
		
		try
		{
			userBean = (UserBean) this.getSubject().getPrincipal();
		}
		catch (Exception e)
		{
			logger.error("", e);
		}
		return userBean;
	}
	
	protected long getAdminId()
	{
		UserBean userBean = this.getUserBean();
		if (userBean == null)
			userBean = new UserBean();
		return userBean.getAdminId();
	}
	
	protected long getUserId()
	{
		UserBean userBean = this.getUserBean();
		if (userBean == null)
			userBean = new UserBean();
		return userBean.getUserId();
	}
	
	protected String getMobile()
	{
		UserBean userBean = this.getUserBean();
		if (userBean == null)
			userBean = new UserBean();
		return userBean.getMobile();
	}
	
	/**
	 * 获取分页信息
	 * @return
	 */
	protected <T> Page<T> getPageInfo()
	{
		return this.getPageInfo("", true);
	}
	
	/**
	 * 获取分页信息
	 * @param defaultField	默认排序方式字段
	 * @param defaultOrder	是否升序
	 * @return
	 */
	protected <T> Page<T> getPageInfo(String defaultField, boolean defaultOrder)
	{
		int pageNum = Tools.genInt(request.getParameter("pageNum"));
		if (pageNum <= 0) pageNum = 1;
		int pageSize = Tools.genInt(request.getParameter("pageSize"));
		if (pageSize <= 0) pageSize = Constant.PageSize;
		Page<T> page = new Page<T>(pageNum, pageSize);
		String field = request.getParameter("field");
		String order = request.getParameter("order");
		if (!Tools.isTrimEmpty(field) && !Tools.isTrimEmpty(order))
		{
			page.setOrderByField(field);
			page.setAsc(order.equals("asc") ? true : false);
		}
		else
		{
			if (!Tools.isTrimEmpty(defaultField))
			{
				page.setOrderByField(defaultField);
				page.setAsc(defaultOrder);
			}
		}
		return page;
	}
	
}
